package myApp.KnorrAndroid.model;


import java.util.Vector;

import android.webkit.JsPromptResult;

import myApp.Constant.ActionEventConstant;
import myApp.Constant.ErrorConstants;
import myApp.KnorrAndroid.common.ActionEvent;
import myApp.KnorrAndroid.common.ModelEvent;
import myApp.KnorrAndroid.controller.UserController;
import myApp.KnorrAndroid.dto.UserDTO;
import myApp.KnorrAndroid.lib.json.JSONException;
import myApp.KnorrAndroid.lib.json.JSONObject;
import myApp.KnorrAndroid.lib.network.http.HTTPMessage;
import myApp.KnorrAndroid.lib.network.http.HTTPRequest;
import myApp.KnorrAndroid.lib.network.http.HTTPResponse;
import myApp.KnorrAndroid.ultils.StringUtil;

public class UserModel extends AbstractModelService {
	static UserModel instance;

	protected UserModel() {
	}

	public static UserModel getInstance() {
		if (instance == null) {
			instance = new UserModel();
		}
		return instance;
	}
	
	
	@Override
	public void onReceiveData(HTTPMessage mes) {
		ActionEvent actionEvent = (ActionEvent) mes.getUserData();
		ModelEvent model = new ModelEvent();
		model.setDataText(mes.getDataText());
		model.setParams(((HTTPResponse) mes).getRequest().getDataText());
		model.setActionEvent(actionEvent);
		
		if (StringUtil.isNullOrEmpty((String) mes.getDataText())) {
			model.setModelCode(ErrorConstants.ERROR_COMMON);
			UserController.getInstance().handleErrorModelEvent(model);
			return;
		}
		JSONObject json;//json du lieu tra ve
		switch (mes.getAction()) {
		case ActionEventConstant.LOGIN:
			try {
				json = new JSONObject((String) mes.getDataText());
				JSONObject result = json.getJSONObject("result");
				int errCode = result.getInt("errorCode");
				model.setModelCode(errCode);
				JSONObject response = result.getJSONObject("response");
				if (errCode == ActionEventConstant.ERROR_CODE_SUCCESS) {
					model.setModelData(parsetLOGIN(response));					
					UserController.getInstance().handleModelEvent(model);
				} else {
					UserController.getInstance().handleErrorModelEvent(model);
				}
			} catch (JSONException e) {
				model.setModelCode(ErrorConstants.ERROR_COMMON);
				UserController.getInstance().handleErrorModelEvent(model);
			}
			break;
			
		case ActionEventConstant.REGISTER:
			try {
				json = new JSONObject((String) mes.getDataText());
				JSONObject result = json.getJSONObject("result");
				int errCode = result.getInt("errorCode");
				model.setModelCode(errCode);
				JSONObject response = result.getJSONObject("response");
				if (errCode == ActionEventConstant.ERROR_CODE_SUCCESS) {
					model.setModelData(parseRegister(response));					
					UserController.getInstance().handleModelEvent(model);
				} else {
					UserController.getInstance().handleErrorModelEvent(model);
				}
			} catch (JSONException e) {
				model.setModelCode(ErrorConstants.ERROR_COMMON);
				UserController.getInstance().handleErrorModelEvent(model);
			}
			break;

		default:
			break;
		}
	}
	
	
	/**
	 *  parse du lieu sau khi login thanh cong
	 *  @author: BangHN
	 *  @version: 1.0
	 *  @since: Jan 1, 2012
	 */
	public UserDTO parsetLOGIN(JSONObject json) {
		UserDTO us = new UserDTO();
		us.parseFromJSONLogin(json);
		return us;
	}
	
	
	/**
	 *  parse du lieu sau khi dang ky thanh cong
	 *  @author: BangHN
	 *  @version: 1.0
	 *  @since: Jan 1, 2012
	 */
	public UserDTO parseRegister(JSONObject json){
		UserDTO us = new UserDTO();
		us.parseFromJSONLogin(json);
		return us;
	}
	
	
	
	@Override
	public void onReceiveError(HTTPResponse mes) {
		// TODO Auto-generated method stub
		ActionEvent actionEvent = (ActionEvent) mes.getUserData();
		ModelEvent model = new ModelEvent();
		model.setDataText(mes.getDataText());
		model.setParams(((HTTPResponse) mes).getRequest().getDataText());
		model.setActionEvent(actionEvent);

		if (StringUtil.isNullOrEmpty((String) mes.getDataText())) {
			model.setModelCode(ErrorConstants.ERROR_COMMON);
			UserController.getInstance().handleErrorModelEvent(model);
			return;
		}
		switch (mes.getAction()) {
		case ActionEventConstant.LOGIN:
			break;
		
		}
	}

	
	/**
	 *	request login,  
	 *  @author: BangHN
	 *  @version: 1.0
	 *  @since: Jan 1, 2012
	 */
	@SuppressWarnings("rawtypes")
	public HTTPRequest requestLoginHTTP(ActionEvent e) {
		return sendHttpRequest("authenticate.login", (Vector)e.viewData, e);
	}
	
	
	/**
	 *	request login,  
	 *  @author: BangHN
	 *  @version: 1.0
	 *  @since: Jan 1, 2012
	 */
	@SuppressWarnings("rawtypes")
	public HTTPRequest requestRegister(ActionEvent e) {
		return sendHttpRequest("authenticate.register", (Vector)e.viewData, e);
	}
}
